class Solution
{
public:
    int maxSumRangeQuery(vector<int> &nums, vector<vector<int>> &requests)
    {
        int n = nums.size();
        vector<long long> countDiff(n + 1, 0);
        for (auto &request : requests)
        {
            ++countDiff[request[0]];
            --countDiff[request[1] + 1];
        }
        partial_sum(countDiff.begin(), countDiff.end(), countDiff.begin());
        sort(countDiff.begin(), countDiff.end());
        sort(nums.begin(), nums.end());
        long long result = 0;
        for (int i = 0; i < n; ++i)
        {
            result += nums[i] * countDiff[i + 1];
            result %= 1'0000'0000'7;
        }
        return result;
    }
};